/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.service;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.SHA256Utils;
import com.guoyu.project.dao.ManagerDao;
import com.guoyu.project.model.Manager;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 管理员,业务类ManagerService
 *
 * @author: xblibo Sun Jun 07 17:40:03 CST 2015
 * @version 1.0
 */
@Service
public class ManagerService {

    @Resource
    private ManagerDao managerDao;

    /**
     * 登录
     * 
     * @param name
     * @param password
     * @return
     */
    public Manager login(String name, String password) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("name", name);
        params.put("password", SHA256Utils.getSHA256String(password));
//        params.put("password", DigestUtils.md5Hex(password));
        List<Manager> managers = managerDao.queryManagers(params);
        if (CollectionUtils.isNotEmpty(managers)) {
            return managers.get(0);
        }
        return null;
    }

    /**
     * 保存管理员数据
     *
     * @param manager
     */
    public void saveManager(Manager manager) {
//        manager.setPassword(DigestUtils.md5Hex(manager.getPassword()));
        manager.setPassword(SHA256Utils.getSHA256String(manager.getPassword()));
        managerDao.save(manager);
    }

    /**
     * 更新管理员实体
     *
     * @param manager
     */
    public void updateManager(Manager manager) {
        if (StringUtils.isNoneBlank(manager.getPassword())) {
            manager.setPassword(SHA256Utils.getSHA256String(manager.getPassword()));
//            manager.setPassword(DigestUtils.md5Hex(manager.getPassword()));
        }
        managerDao.update(manager);
    }

    /**
     * 删除管理员数据
     *
     * @param managerId
     */
    public void deleteById(int managerId) {
        managerDao.deleteById(managerId);
    }

    /**
     * 根据id获取管理员数据
     *
     * @param managerId
     * @return
     */
    public Manager queryById(int managerId) {
        return managerDao.queryById(managerId);
    }

    /**
     * 根据条件分页查询管理员数据
     *
     * @param offset
     * @param limit
     * @param params
     * @return
     */
    public Pagination<Manager> queryManagers(int offset, int limit, Map<String, Object> params) {
        // 根据条件获取总记录数
        int total = managerDao.queryManagerCount(params);

        // 构建分页对象，参数
        if (null == params) {
            params = Maps.newHashMap();
        }
        params.put(Pagination.OFFSET, offset);
        params.put(Pagination.LIMIT, limit);

        // 分页查询数据
        List<Manager> managerList = managerDao.queryManagers(params);

        return new Pagination<Manager>(offset, limit, total, managerList);
    }

    /**
     * 修改管理员信息
     * @param manager
     */
    public void update(Manager manager) {
        managerDao.update(manager);
    }

    /**
     * 查询当前用户是否存在
     */
    public Manager userExist(String name){
        Map<String, Object> params = Maps.newHashMap();
        params.put("name", name);
        List<Manager> managers = managerDao.queryManagers(params);
        if (CollectionUtils.isNotEmpty(managers)) {
            return managers.get(0);

        }
        return null;

    }

    /**
     * 登录以md5加密
     *
     * @param name
     * @param password
     * @return
     */
    public Manager loginByMD5(String name, String password) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("name", name);
//        params.put("password", SHA256Utils.getSHA256String(password));
        params.put("password", DigestUtils.md5Hex(password));
        List<Manager> managers = managerDao.queryManagers(params);
        if (CollectionUtils.isNotEmpty(managers)) {
            return managers.get(0);

        }
        return null;
    }

}